/*
A Pandemic Crossing the Border: The Impact of Covid-19 in the US on the Mexican Labor Market
Rojas and Yu, 2023

Replication file for Tables 2, 3, A1, A2, A3, and A4
*/

clear all
set more off

global data "COMPLETE YOUR DIRECTORY HERE \data"
global output "COMPLETE YOUR DIRECTORY HERE \output"

use "$data/cleaned_ind_daily.dta", clear

destring monthincomeifwork whrsifwork hrwageifwork, replace force

gen t=.
replace t=1 if qtr==12019
replace t=2 if qtr==22019
replace t=3 if qtr==32019
replace t=4 if qtr==42019
replace t=5 if qtr==12020
replace t=6 if qtr==32020
replace t=7 if qtr==42020

label define quarter_lab 1 "Q1-2019" 2 "Q2-2019" 3 "Q3-2019" 4 "Q4-2019" 5 "Q1-2020" 6 "Q3-2020" 7 "Q4-2020"
label values t quarter_lab

egen id2=group(id)
egen t2=group(int_date)

drop if telephone==1
drop if missing_emif==1

xtset id2 t

**Municipality size
gen loc_big=0
replace loc_big=1 if pop>200000

**Migration intensity (migrants_rate_q is the migrants rate quantile)
gen migrants_high=(migrants_rate_q=="q3" | migrants_rate_q=="q4" | migrants2010_rate_q=="q3" | migrants2010_rate_q=="q4")

keep if status1=="active"

/*
Table 2
*/

preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx if hrwage>0 [aw=weight] , by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe hrwage covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_h1
reghdfe hrwage covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_h2
restore

preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx [aw=weight], by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_w1
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_w2
restore

/*
Table 3
*/

**Males
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx if female==0 [aw=weight], by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store ma_w1
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store ma_w2
restore

**Females
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx if female==1 [aw=weight], by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store fe_w1
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fe_w2
restore

**Agriculture
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx if occ=="agriculture" [aw=weight], by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store ag_w1
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store ag_w2
restore

**Non-agriculture
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx if occ!="agriculture" [aw=weight], by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store na_w1
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store na_w2
restore

**High migration
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx if migrants_high==1 [aw=weight], by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store mh_w1
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store mh_w2
restore

**Low migration
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx if migrants_high==0 [aw=weight], by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store ml_w1
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store ml_w2
restore


/*
Table A1
*/

**Large municipalities
preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx loc_big if hrwage>0 [aw=weight] , by(mx_mun t)
keep if loc_big==1
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe hrwage covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_big_h1
reghdfe hrwage covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_big_h2
restore

preserve
collapse (mean) hrwage whrs covid_exp_w covid_exp_mx loc_big [aw=weight], by(mx_mun t)
keep if loc_big==1
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_big_w1
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_big_w2
restore

/*
Table A2
*/

**Use 2010 migration rate
preserve
collapse (mean) hrwage whrs covid_exp_w2010 covid_exp_mx if hrwage>0 [aw=weight] , by(mx_mun t)
label var covid_exp_w2010 "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe hrwage covid_exp_w2010, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_2010_h1
reghdfe hrwage covid_exp_w2010 covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_2010_h2
restore

preserve
collapse (mean) hrwage whrs covid_exp_w2010 covid_exp_mx [aw=weight], by(mx_mun t)
label var covid_exp_w2010 "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w2010, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_2010_w1
reghdfe whrs covid_exp_w2010 covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_2010_w2
restore

/*
Table A3
*/

**Controlling for Mexico covid-19 deaths
preserve
collapse (mean) hrwage whrs si_exp_w covid_exp_w covid_death_mx if hrwage>0 [aw=weight] , by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_death_mx "Mexico's Covid-19 prevalence"
reghdfe hrwage covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_death_h1
reghdfe hrwage covid_exp_w covid_death_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_death_h2
restore

preserve
collapse (mean) hrwage whrs si_exp_w covid_exp_w covid_death_mx [aw=weight], by(mx_mun t)
label var covid_exp_w "US Covid-19 exposure"
label var covid_death_mx "Mexico's Covid-19 prevalence"
reghdfe whrs covid_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store |
reghdfe whrs covid_exp_w covid_death_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_death_w2
restore

/*
Table A4
*/

**US stringency index
preserve
collapse (mean) hrwage whrs si_exp_w covid_exp_w covid_exp_mx if hrwage>0 [aw=weight] , by(mx_mun t)
label var si_exp_w "US Covid-19 Policy Stringency"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe hrwage si_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_si_h1
reghdfe hrwage si_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_si_h2
restore

preserve
collapse (mean) hrwage whrs si_exp_w covid_exp_w covid_exp_mx [aw=weight], by(mx_mun t)
label var si_exp_w "US Covid-19 Policy Stringency"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
reghdfe whrs si_exp_w, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_si_w1
reghdfe whrs si_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
estimates store fs_si_w2
restore




*Create tables			
cd "$output"
			
**Table 2
label var covid_exp_w "US Covid-19 exposure"
label var covid_exp_mx "Mexico's Covid-19 prevalence"
esttab fs_w1 fs_w2 fs_h1 fs_h2 using "table_2.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N, fmt(%9.0gc)) replace ///
	title("Effects of US covid exposure on wages and worked hours") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{All\\Hours}" "\shortstack{All\\ Hours}""\shortstack{All\\Wages}" "\shortstack{All\\ Wages}")
	
**Table 3
**Gender
esttab ma_w1 ma_w2 fe_w1 fe_w2 using "table_3_a.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N, fmt(%9.0gc)) replace ///
	title("Effects of US covid exposure on wages and worked hours by gender") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle( "\shortstack{Males\\Hours}" "\shortstack{Males\\Hours}" "\shortstack{Females\\Hours}" "\shortstack{Females\\Hours}")
	
**Occupations
esttab ag_w1 ag_w2 na_w1 na_w2 using "table_3_b.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N, fmt(%9.0gc)) replace ///
	title("Effects of US covid exposure on wages and worked hours by type of occupation") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{Agriculture\\Hours}" "\shortstack{Agriculture\\Hours}" "\shortstack{Non-agriculture\\Hours}" "\shortstack{Non-agriculture\\Hours}") 
		
**Migration intensity
esttab mh_w1 mh_w2 ml_w1 ml_w2 using "table_3_c.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N, fmt(%9.0gc)) replace ///
	title("Effects of US covid exposure on wages and worked hours by migration intensity") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{High migration\\Hours}" "\shortstack{High migration\\Hours}" "\shortstack{Low migration\\Hours}" "\shortstack{Low migration\\Hours}") 	

	
**Table A1
**Big localities
	esttab fs_big_w1 fs_big_w2 fs_big_h1 fs_big_h2 using "table_A1.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N, fmt(%9.0gc)) replace ///
	title("Effects of US covid restrictions on wages and worked hours") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{All\\Hours}" "\shortstack{All\\ Hours}""\shortstack{All\\Wages}" "\shortstack{All\\ Wages}")
	
	
**Table A2
**Using 2010 migration rates
	esttab fs_2010_w1 fs_2010_w2 fs_2010_h1 fs_2010_h2 using "table_A2.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N, fmt(%9.0gc)) replace ///
	title("Effects of US covid restrictions on wages and worked hours") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{All\\Hours}" "\shortstack{All\\ Hours}""\shortstack{All\\Wages}" "\shortstack{All\\ Wages}")
	
**Table A3
**Controlling for Mexico Covid-19 deaths
	esttab fs_death_w1 fs_death_w2 fs_death_h1 fs_death_h2 using "table_A3.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N, fmt(%9.0gc)) replace ///
	title("Effects of US covid restrictions on wages and worked hours") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{All\\Hours}" "\shortstack{All\\ Hours}""\shortstack{All\\Wages}" "\shortstack{All\\ Wages}")

**Table A4
**US stringency index
	esttab fs_si_w1 fs_si_w2 fs_si_h1 fs_si_h2 using "table_A4.tex", cells(b(star fmt(a3))  se(par(`"("'`")"')) )  starlevels(* .1 ** .05 *** .01) stats(N, fmt(%9.0gc)) replace ///
	title("Effects of US covid restrictions on wages and worked hours") ///
	note("Note: standard errors clustered at the municipality level in parentheses. Significance codes are * 0.1, ** 0.05, *** 0.01") ///
	mtitle("\shortstack{All\\Hours}" "\shortstack{All\\ Hours}""\shortstack{All\\Wages}" "\shortstack{All\\ Wages}")
	